<?php

namespace app\modules\search\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Helpers;
use app\modules\clinical\models\Patient;

class SearchSenior extends Patient
{
	public $dbs;
	public $age_rel;
	public $med_rel;
	public $med_name;
    public $med_ammout;
    public $_pt_label;
    public function rules()
    {
        // 只有在 rules() 函数中声明的字段才可以搜索
        return [
            [['pt_history', 'pt_label', 'crt_name'], 'string', 'max' => 255],
            [['age_rel', 'med_rel', 'med_name', 'med_ammout', 'pt_age', 'dbs'], 'integer'],
        ];
    }

    public function search($params)
    {
        $query = Patient::find()->select('*')->where(['hspt_patient.delete_at' => 0]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'crt_date' => SORT_DESC,
                ]
            ],
        ]);

        // 从参数的数据中加载过滤条件，并验证
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        // 增加过滤条件来调整查询对象 excel_pd
        $rel = self::values();
        if($this->dbs || $this->med_ammout != '') {
            $query->innerJoin('hspt_crtinfo', 'hspt_crtinfo.pt_id = hspt_patient.pk_id');
            $query->innerJoin('excel_pd', 'excel_pd.pk_id = hspt_crtinfo.excel_id');
            $query->andWhere(['hspt_crtinfo.pt_diagnose' => 'pd']);

            if ($this->dbs == '1') {
            	$query->addSelect('hspt_crtinfo.excel_id AS dbs_id, hspt_crtinfo.crt_time AS dbs_time');
        		$query->andFilterWhere(['excel_pd.treatment' => 0]);
        	}

        	if ($this->med_ammout != '') {
            	$query->addSelect('med_ctn.excel_id AS med_id');
        		$query->innerJoin('med_ctn', 'med_ctn.excel_id = excel_pd.pk_id');
        		$query->andWhere(['med_ctn.med_id' => $this->med_name]);
        		$count = "(`time6`+`time7`+`time8`+`time9`+`time10`+`time11`+`time12`+`time13`+`time14`+`time15`+`time16`+`time17`+`time18`+`time19`+`time20`)";

        		($this->med_rel == '0') ? $query->andFilterWhere([$count => $this->med_ammout]) : $query->andFilterWhere([$rel[$this->med_rel], $count, intval($this->med_ammout)]);
        	}
        }

        // 增加过滤条件来调整查询对象
        (empty($this->age_rel) || $this->age_rel == '0') ? $query->andFilterWhere(['pt_age' => $this->pt_age]) : $query->andFilterWhere([$rel[$this->age_rel], 'pt_age', $this->pt_age]);

        $pt_history = Helpers::clearStringForArray($this->pt_history, true);
        $pt_label = Helpers::clearStringForArray($this->pt_label, true);

        foreach ($pt_history as $history) {
            $query->andFilterWhere(['REGEXP', 'pt_history', "^{$history}$|\+{$history}\+|\+{$history}$|^{$history}\+"]);
        }

        foreach ($pt_label as $label) {
            $query->andFilterWhere(['REGEXP', 'pt_label', "^{$label}$|\+{$label}\+|\+{$label}$|^{$label}\+"]);
        }

        $query->andFilterWhere(['=', 'hspt_patient.crt_name', $this->crt_name]);

        return $dataProvider;
    }

    static public function values()
    {
        $result = ['=', '>=', '<=', '>', '<'];
    	
    	return $result;
    }
}